import util from 'util'
import {Permission} from './resource'

export const deleteConfirm = (vue, options, deleteFunc, success, fail,cancel) => {
  let defaulOp = {
    _title: '您确定?',
    _content: '删除的记录将不能恢复!',
    confirmButtonText: '是的, 我要删除!',
    cancelButtonText: '取消',
    type: 'warning'
  }
  let op = Object.assign(defaulOp, options)
  return vue.$confirm(op._content, op._title, op).then(() => {
    return deleteFunc()
  }).then(response => {
    if (response.data.success) {
      vue.$message({
        type: 'success',
        message: '删除成功!',
      })
      success(response)
    } else {
      vue.$message({
        type: 'error',
        message: '删除失败!',
      })
      fail(response)
    }
  }).catch(() => {
    if(cancel) {
      return cancel()
    }
  })
}

export const validateForm = (vue, formName, success, fail) => {
  return vue.$refs[formName].validate((valid) => {
    if (valid) {
      if (success) {
        success()
      }
    } else {
      if (fail) {
        fail()
      } else {
        console.log('validate failed!!')
      }
    }
  })
}

export const showSaveResponse = (vue, saveFunc, success, fail) => {
  vue.loading = true
  saveFunc().then(res => {
    vue.loading = false
    if (res.data.success) {
      vue.$message({
        type: 'success',
        message: '保存成功!'
      })
      if (success) {
        setTimeout(() => success(), 500)
      }

    } else {
      vue.$message({
        type: 'error',
        message: '保存失败'
      })
      if (fail) {
        setTimeout(() => fail(), 500)
      }
    }
  })
}

export const validFieldExists = (rule, value, callback, isValid, validFunc, failFunc) => {
  let fieldName = rule.field
  if (!isValid) {
    callback()
  } else {
    new Promise((resolve) => {
      resolve(validFunc(rule, value))
    }).then(res => {
      if (!res.data.valid) {
        callback()
      } else {
        if (failFunc) {
          failFunc(callback, res.data)
        } else {
          let errorMsg = util.isNull(res.data.msg) ? `字段: ${fieldName} 已存在` : res.data.msg
          callback(new Error(errorMsg))
        }
      }
    })
  }
}

export const SessionStorageUtil = {
  getItem: key => {
    try {
      let data = JSON.parse(sessionStorage.getItem(key))
      return data == null ? undefined : data
    } catch (e) {
      console.log(e)
      return undefined
    }
  },
  setItem: (key, value) => {
    sessionStorage.setItem(key, JSON.stringify(value))
  }
}

export const getMenuTree = async () => {
  let res = await Permission.listMenuTree()
  if (res.data.success) {
    let menus = res.data.data
    let menusL1 = []
    menusL1.push({
      componentName: "Dashboard",
      code: "dashboard",
      name: "工作台",
      level: 1,
      parent: 0,
      children:[],
    })
    return menusL1.concat(menus)
  }
}
export const getMenus = async () => {
  let res = await Permission.listMenus()
  if (res.data.success) {
    let menus = res.data.data
    let menusL1 = []
    menusL1.push({
      name: "Dashboard",
      path: "/dashboard",
      title: "工作台",
      level: 1
    })
    for (let i = 0; i < menus.length; i++) {
      let menu = menus[i];
      if (menu.level === 1) {
        menusL1.push(menu)
      }
    }
    return menusL1
  }
}

